

import { heartbeat, login } from "@/api/index"

// 定义一个 JavaScript 类
export default class LoginLogic {
    constructor() {
        this.id = null
        this.uuid = null
        this.callback = null
        this.intervals = null
        this.token = null
        this.number = 0
        this.messageIds = []
    }

    setCallback(callback) {
        this.callback = callback;
    }

    setMessageId(messageId) {
        this.messageIds.push(messageId)
    }

    /**
    * 登录成功
    * **/
    async loginSuccess(id,uuid) {
        this.id = id;
        this.uuid = uuid;
        // this.id="123"
        // uuid="123456_ABC"
        login({
            serialNumber: uuid,
            wechatId: this.id,
            loginTime: new Date().getTime(),
        }).then(res => {
            console.log("登录成功信息", res)
            if( res.success){
                if (this.callback) {
                    res.data.wxName = this.wxName
                    res.data.wxId = id
                    this.callback.onLoginSuccess(res.data)
                }
                this.token = res.data.token
                this.startLoop();
            }else{
                if (this.callback) {
                    this.callback.onLoginError(res.errorMsg)
                }
            }
           
        }).catch(err => {
            console.log("错误信息", err)
            if (this.callback) {
                this.callback.onLoginError(err)
            }
        })
    }

    /**
   /**
    * 开启循环
    * **/
    async startLoop() {
        console.log("开启心跳");
        if (this.intervals) {
            clearInterval(this.intervals);
        }
        this.intervals = setInterval(() => {
            console.log("执行心跳");
            if (this.callback) {
                this.callback.onHeartBeat()
            }
            this.sendHeartbeat();
        }, 1000 * 10, 0);
    }

    /**
   * 发送心跳包 
   **/
    sendHeartbeat() {
        var sendMessageIds = this.messageIds

        heartbeat({
            serialNumber: this.uuid,
            wechatId: this.id,
            messagesIds: sendMessageIds,
            loginTime: new Date().getTime(),
        }, this.token).then(res => {
            if (res.success) {
                this.number = 0
                this.messageIds = this.messageIds.filter(item => !sendMessageIds.includes(item));
                const messages = JSON.parse(res.data.messages)
                if (this.callback && messages.length > 0) {
                    this.callback.onMessageList(messages)
                }
            } else {
                this.number++
                if (this.number >= 3) {
                    this.number = 0
                    if (this.intervals) {
                        clearInterval(this.intervals);
                    }
                    this.loginSuccess(this.id,this.uuid)
                }
            }


        }).catch(err => {
            console.log(err);
            this.number++
            if (this.number >= 3) {
                this.number = 0
                if (this.intervals) {
                    clearInterval(this.intervals);
                }
                this.loginSuccess(this.id,this.uuid)
            }
        })
    }


    outLogin() {
        this.number = 0
        if (this.intervals) {
            clearInterval(this.intervals);
        }
    }


}